<?php

/**
 * RssController
 * 
 * @package Sgit 
 * @author Juan Manuel Martinez Carrillo
 * @copyright Juan Manuel Martinez Carrillo
 * @version 2012
 * @access public
 * @revision 17/02/2012 8:53:55
 */

class RssController extends Sgit_Controller_Action
{
    protected $_checkAcceso=false;

    protected $_add_users_all = array(32, //MAM
        34, //SHO
        82, //RCC
        99, //Rai
        100, //Javier campo
        106, //Lucia
        166, //J LLosa
        118, //Piscione

    );

    function _begin()
    {

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $this->_loadModel('OfertasPost');
        $this->_loadModel('Ofertas');


        $server = "http://" . $this->getRequest()->getServer('SERVER_NAME') . '/';


        $this->feed = new Zend_Feed_Writer_Feed;
        $this->feed->setTitle('Viriathus');
        $this->feed->setLink($server);
        $this->feed->setFeedLink($server . 'rss', 'atom');
        $this->feed->addAuthor(array('name' => 'Viriathus', 'uri' => $server, ));
        $this->feed->setDateModified(time());
        $this->feed->addHub($server);

    }


    public function ofertasAction()
    {

        $this->feed->setTitle('Viriathus Ofertas');
        
        $ofertas = $this->_model->Ofertas->getOfertas();
        $server = "http://" . $this->getRequest()->getServer('SERVER_NAME') . '/';

        foreach ($ofertas as $oferta)
        {
            if ($this->_checkPermiso($oferta['accesos']))
                $ofertas_permitidas[] = $oferta['id'];
            
            $titulo[$oferta['id']] = $oferta['cliente'];
            $zonas[$oferta['id']] = $oferta['zona'];

        }


        $rows = $this->_model->OfertasPost->getPostsByList($ofertas_permitidas);

        foreach ($rows as $v)
        {
            $usuario = $v['usuario'] ? $v['usuario'] : '_';

            $pid = 'OF' . str_pad($v['id_oferta'], 4, "0", STR_PAD_LEFT);


            $notas=$v['usuario'].'<br>';
            $notas.=$this->view->text($v['notas']);

            $entry = $this->feed->createEntry();
            $entry->setTitle($zonas[$v['id_oferta']].' '.$pid . ' ' . $titulo[$v['id_oferta']]);
            $entry->setLink($server . 'ofertas-post/index/id_oferta/' . $v['id_oferta'] . '#' . $v['id']);
            $entry->setDateModified(strtotime($v['fecha']));
            $entry->setDateCreated(strtotime($v['fecha']));
            
            $entry->setDescription($notas);
            $this->feed->addEntry($entry);
        }


        $out = $this->feed->export('atom');
        print $out;


    }
    
    
    
    public function ofertaAction()
    {

        $id=$this->_getParam('id');

        $oferta = $this->_model->Ofertas->getOferta($id);
        
        $server = "http://" . $this->getRequest()->getServer('SERVER_NAME') . '/';



        if (!$this->_checkPermiso($oferta['accesos']))
            exit('No tienes acceso');
           
            
        $pid = 'OF' . str_pad($oferta['id'], 4, "0", STR_PAD_LEFT);    
        $titulo =$oferta['zona'].' '.$pid.' ' .$oferta['cliente'];

        unset($this->feed);
        
        
        
        $this->feed = new Zend_Feed_Writer_Feed;
        $this->feed->setTitle($oferta['cliente']);
        $this->feed->setLink($server);
        $this->feed->setFeedLink($server . 'rss/oferta/id/'.$oferta['id'], 'atom');
        $this->feed->addAuthor(array('name' => 'Viriathus', 'uri' => $server . 'rss/oferta/id/'.$oferta['id'], ));
        $this->feed->setDateModified(time());
        $this->feed->addHub($server . 'rss/oferta/id/'.$oferta['id']);


        $rows = $this->_model->OfertasPost->getPostsByList($oferta['id']);

        foreach ($rows as $v)
        {
            $usuario = $v['usuario'] ? $v['usuario'] : '_';


            $notas=$v['usuario'].'<br>';
            $notas.=$this->view->text($v['notas']);

            $entry = $this->feed->createEntry();
            $entry->setTitle($titulo);
            $entry->setLink($server . 'ofertas-post/index/id_oferta/' . $v['id_oferta'] . '#' . $v['id']);
            $entry->setDateModified(strtotime($v['fecha']));
            $entry->setDateCreated(strtotime($v['fecha']));
            
            $entry->setDescription($notas);
            $this->feed->addEntry($entry);
        }


        $out = $this->feed->export('atom');
        print $out;


    }


    /**
     * The default action for the rss controller
     * Which shows the rss document
     */
    public function indexAction()
    {


        /**
         * Add one or more entries. Note that entries must
         * be manually added once created.
         */

        $entry = $this->feed->createEntry();
        $entry->setTitle('Test');
        $entry->setLink('http://172.19.4.136');
        $entry->addAuthor(array('name' => 'Paddy', 'email' => 'paddy@example.com', 'uri' => 'http://www.example.com', ));
        $entry->setDateModified(time());
        $entry->setDateCreated(time());
        $entry->setDescription('Exposing the difficultly of porting games to English.');
        $entry->setContent('I am not writing the article. The example is long enough as is ;).');

        $this->feed->addEntry($entry);


        /**
         * Render the resulting feed to Atom 1.0 and assign to $out.
         * You can substitute "atom" with "rss" to generate an RSS 2.0 feed.
         */

        $out = $this->feed->export('atom');
        print $out;

    }


    public function _checkPermiso($accesos)
    {

        //esta en lista acceso
        $validator = new Zend_Validate_InArray(unserialize($accesos));
        if ($validator->isValid($this->_auth->getIdentity()->id))
        {
            return true;
        }


        //Esta en la lista de usuarios con acceso a todo
        $validator = new Zend_Validate_InArray($this->_add_users_all);
        if ($validator->isValid($this->_auth->getIdentity()->id))
        {
            return true;
        }


        if (is_null($this->_esSuperUser))
        {
            //este en la lista de acceso o que sea superuser
            $validatorsup = new Sgit_Validate_isSuperuser();
            $this->_esSuperUser = $validatorsup->isValid();
        }

        if ($this->_esSuperUser === true)
            return true;


        return false;

    }


}
